/****************************************************************************************************
Copyright (C) 2008 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/
/*globals Application, CheckBox, ColorDialog, ComboBox, Dialog, Env, FontDialog, GroupBox, Label, mnDebug, MultipletReporter, PeakReporter, settings, SpinBox*/
/*jslint plusplus: true, indent: 4, bitwise: true*/


/***********************************************************
 *														   *
 *					MULTIPLET REPORT DIALOG				   *
 *														   *
 ***********************************************************/

/* This function defines the constructor of the dialog to create Multiplet Reports  */
function MultipletReportDialog(aArrayOfReporters, aSaveSettings) {
	'use strict';

	var dialog, font, i;

	this.ui = Application.loadUiFile('ricares:WMultipletReportDialogSetup.ui');
	dialog = this.ui.widgets;

	this.fSaveSettings = aSaveSettings;
	for (i = 0; i < aArrayOfReporters.length; i++) {
		this.addReporter(aArrayOfReporters[i]);
	}

	this.fProps = MultipletReporter.getSettings();

	dialog.cmbReporter.currentIndex = Math.max(0, dialog.cmbReporter.items.indexOf(this.fProps.reporterName));
	dialog.cbAllAsRanges.checked = this.fProps.allAsRanges;
	dialog.cbPentapletsAsPent.checked = this.fProps.pentapletsAsPent;
	dialog.fMultipletsCheckBox.checked = this.fProps.reportRange;
	dialog.fMultipletsSortCheckBox.checked = this.fProps.sortMultipletsAscending ;
	dialog.fSortJsAscendingCheckBox.checked = this.fProps.sortJAscending;
	dialog.fReduceJListCheckBox.checked = this.fProps.reducedJList;
	dialog.fSolventNameCheckBox.checked = this.fProps.extendedSolventName;
	dialog.fReportJsCheckBox.checked = this.fProps.reportJs;
	dialog.fReportAssignmentsCheckBox.checked = this.fProps.reportAssignments;
	dialog.fDeltaPrecisionSpinBox.value = this.fProps.deltaPrecision;
	dialog.fJsPrecisionSpinBox.value = this.fProps.jPrecision;
	dialog.cmbFillStyle.currentText = this.fProps.fillStyle;
	dialog.lbFont.setFont(this.fProps.fontProperties);
	dialog.lbFont.text = this.fProps.fontProperties.family;
	dialog.pbColor.setIconColoredSquare(this.fProps.fontColor);
	dialog.fHtmlCheckBox.checked = this.fProps.useHtml;

	// This is necessary to keep compatibility with scripts that use the old interface of the report dialog
	this.fDiag = this.ui;
	this.cmbReporter = dialog.cmbReporter;
	this.cbAllAsRanges = dialog.cbAllAsRanges;
	this.cbPentapletsAsPent = dialog.cbPentapletsAsPent;
	this.fMultipletsCheckBox = dialog.fMultipletsCheckBox;
	this.fMultipletsSortCheckBox = dialog.fMultipletsSortCheckBox;
	this.fSortJsAscendingCheckBox = dialog.fSortJsAscendingCheckBox;
	this.fReduceJListCheckBox = dialog.fReduceJListCheckBox;
	this.fSolventNameCheckBox = dialog.fSolventNameCheckBox;
	this.fReportJsCheckBox = dialog.fReportJsCheckBox;
	this.fReportAssignmentsCheckBox = dialog.fReportAssignmentsCheckBox;
	this.fDeltaPrecisionSpinBox = dialog.fDeltaPrecisionSpinBox;
	this.fJsPrecisionSpinBox = dialog.fJsPrecisionSpinBox;
	this.cmbFillStyle = dialog.cmbFillStyle;
	this.lbFillStyle = dialog.lbFillStyle;

	this.createConnections();
}

MultipletReportDialog.prototype.exec = function () {
	'use strict';

	var dialog = this.ui.widgets,
		retVal = this.ui.exec();

	if (retVal) {
		this.fProps.reporterName = this.getSelectedReporter();
		this.fProps.allAsRanges = dialog.cbAllAsRanges.checked;
		this.fProps.pentapletsAsPent = dialog.cbPentapletsAsPent.checked;
		this.fProps.reportRange = dialog.fMultipletsCheckBox.checked;
		this.fProps.sortMultipletsAscending  = dialog.fMultipletsSortCheckBox.checked;
		this.fProps.sortJAscending = dialog.fSortJsAscendingCheckBox.checked;
		this.fProps.reducedJList = dialog.fReduceJListCheckBox.checked;
		this.fProps.extendedSolventName = dialog.fSolventNameCheckBox.checked;
		this.fProps.reportJs = dialog.fReportJsCheckBox.checked;
		this.fProps.reportAssignments = dialog.fReportAssignmentsCheckBox.checked;
		this.fProps.deltaPrecision = dialog.fDeltaPrecisionSpinBox.value;
		this.fProps.jPrecision = dialog.fJsPrecisionSpinBox.value;
		this.fProps.fillStyle = dialog.cmbFillStyle.currentText;
		this.fProps.useHtml = dialog.fHtmlCheckBox.checked;

		if (this.fSaveSettings) {
			MultipletReporter.setSettings(this.fProps);
		}
	}
	return retVal;
};

MultipletReportDialog.prototype.reporter = function () {
	'use strict';

	var dialog = this.ui.widgets,
		repString = this.getSelectedReporter(),
		reporter = MultipletReporter.getReporterByName(repString);

	reporter.allAsRanges = dialog.cbAllAsRanges.checked;
	reporter.pentapletsAsPent = dialog.cbPentapletsAsPent.checked;
	reporter.reportRange = dialog.fMultipletsCheckBox.checked;
	reporter.sortMultipletsAscending  = dialog.fMultipletsSortCheckBox.checked;
	reporter.sortJAscending = dialog.fSortJsAscendingCheckBox.checked;
	reporter.reducedJList = dialog.fReduceJListCheckBox.checked;
	reporter.extendedSolventName = dialog.fSolventNameCheckBox.checked;
	reporter.reportJs = dialog.fReportJsCheckBox.checked;
	reporter.reportAssignments = dialog.fReportAssignmentsCheckBox.checked;
	reporter.deltaPrecision = dialog.fDeltaPrecisionSpinBox.value;
	reporter.jPrecision = dialog.fJsPrecisionSpinBox.value;
	reporter.fillStyle = dialog.cmbFillStyle.currentText;
	reporter.fontProperties = JSON.parse(JSON.stringify(this.fProps.fontProperties));
	reporter.fontColor = this.fProps.fontColor;
	reporter.useHtml = dialog.fHtmlCheckBox.checked;

	return reporter;
};

MultipletReportDialog.prototype.createConnections = function () {
	'use strict';

	var color, font, dialog = this.ui.widgets;

	dialog.pbColor.clicked.connect(this, function () {
		color = ColorDialog.getColor(this.fProps.fontColor, this.ui, "Select a Color");
		if (color !== undefined) {
			this.fProps.fontColor = color;
			dialog.pbColor.setIconColoredSquare(color);
		}
	});

	dialog.pbFont.clicked.connect(this, function () {
		font = FontDialog.getFont(this.fProps.fontProperties, this.ui, "Select a Font");
		if (font !== undefined) {
			this.fProps.fontProperties = font;
			dialog.lbFont.setFont(font);
			dialog.lbFont.text = font.family;
		}
	});
};

MultipletReportDialog.prototype.addReporter = function (aReporter) {
	'use strict';

	this.ui.widgets.cmbReporter.addItem(aReporter.name);
};

MultipletReportDialog.prototype.getSelectedReporter = function () {
	'use strict';

	return this.ui.widgets.cmbReporter.currentText;
};


/***********************************************************
 *														   *
 *					PEAK REPORT DIALOG					   *
 *														   *
 ***********************************************************/

/* This function defines the constructor of the dialog to create Peak Reports  */
function PeakReportDialog(aArrayOfReporters, aSaveSettings) {
	'use strict';

	var i, dialog;

	this.ui = Application.loadUiFile('ricares:WPeakReportDialogSetup.ui');
	dialog = this.ui.widgets;

	this.fSaveSettings = aSaveSettings;
	for (i = 0; i < aArrayOfReporters.length; i++) {
		dialog.cmbReporter.addItem(aArrayOfReporters[i].name);
	}

	this.fProps = PeakReporter.readSettings();

	dialog.cmbReporter.currentIndex = Math.max(0, dialog.cmbReporter.items.indexOf(this.fProps.reporterName));
	dialog.fSortHAscendingCheckBox.checked = this.fProps.sortHAscending;
	dialog.fSortJsAscendingCheckBox.checked = this.fProps.sortJAscending;
	dialog.fCompoundOnlyCheckBox.checked = this.fProps.compoundOnly;
	dialog.fReport13CAssignmentsCheckBox.checked = this.fProps.report13CAssignments;
	dialog.fDeltaPrecisionSpinBox.value = this.fProps.deltaPrecision;
	dialog.fReportF1CheckBox.checked = this.fProps.fFlags & PeakReporter.ReportF1;
	dialog.fReportF2CheckBox.checked = this.fProps.fFlags & PeakReporter.ReportF2;
	dialog.fReportAsPointsCheckBox.checked = this.fProps.fFlags & PeakReporter.ReportAsPoint;
	dialog.fReportF1CheckBox.enabled = !dialog.fReportAsPointsCheckBox.checked;
	dialog.fReportF2CheckBox.enabled = !dialog.fReportAsPointsCheckBox.checked;
	dialog.fReport13CMultCheckBox.checked = this.fProps.report13CMultiplicity;
	dialog.fSolventNameCheckBox.checked = this.fProps.extendedSolventName;
	dialog.cmbFillStyle.currentText = this.fProps.fillStyle;

	dialog.lbFont.setFont(this.fProps.fontProperties);
	dialog.lbFont.text = this.fProps.fontProperties.family;
	dialog.pbColor.setIconColoredSquare(this.fProps.fontColor);

	this.createConnections();
}

PeakReportDialog.prototype.createConnections = function () {
	'use strict';

	var color, font, dialog = this.ui.widgets;

	dialog.pbColor.clicked.connect(this, function () {
		color = ColorDialog.getColor(this.fProps.fontColor, this.ui, "Select a Color");
		if (color !== undefined) {
			this.fProps.fontColor = color;
			dialog.pbColor.setIconColoredSquare(color);
		}
	});

	dialog.pbFont.clicked.connect(this, function () {
		font = FontDialog.getFont(this.fProps.fontProperties, this.ui, "Select a Font");
		if (font !== undefined) {
			this.fProps.fontProperties = font;
			dialog.lbFont.setFont(font);
			dialog.lbFont.text = font.family;
		}
	});
};

PeakReportDialog.prototype.exec = function () {
	'use strict';

	var dialog = this.ui.widgets,
		retVal = this.ui.exec();

	if (retVal) {
		this.fProps.reporterName = this.getSelectedReporter();
		this.fProps.fFlags = this.getFlags();
		this.fProps.sortHAscending = dialog.fSortHAscendingCheckBox.checked;
		this.fProps.sortJAscending = dialog.fSortJsAscendingCheckBox.checked;
		this.fProps.compoundOnly = dialog.fCompoundOnlyCheckBox.checked;
		this.fProps.report13CAssignments = dialog.fReport13CAssignmentsCheckBox.checked;
		this.fProps.report13CMultiplicity = dialog.fReport13CMultCheckBox.checked;
		this.fProps.deltaPrecision = dialog.fDeltaPrecisionSpinBox.value;
		this.fProps.extendedSolventName = dialog.fSolventNameCheckBox.checked;
		this.fProps.fillStyle = dialog.cmbFillStyle.currentText;

		if (this.fSaveSettings) {
			PeakReporter.writeSettings(this.fProps);
		}
	}
	return retVal;
};

PeakReportDialog.prototype.getSelectedReporter = function () {
	'use strict';

	return this.ui.widgets.cmbReporter.currentText;
};

PeakReportDialog.prototype.getFlags = function () {
	'use strict';

	var flags = 0,
		dialog = this.ui.widgets;

	if (dialog.fReportF1CheckBox.checked) {
		flags |= PeakReporter.ReportF1;
	}
	if (dialog.fReportF2CheckBox.checked) {
		flags |= PeakReporter.ReportF2;
	}
	if (dialog.fReportAsPointsCheckBox.checked) {
		flags |= PeakReporter.ReportAsPoint;
	}

	return flags;
};
